import { fromJS } from "immutable";
import * as constants from "./constants";

const initialState = fromJS({
  topicList: [],
  articleList: [],
  recommendList: [],
  articlePage: 1,
  showBackTop: false
})

const reducer = (state = initialState, action) => {
  const { type, payload } = action;
  switch (type) {
    case constants.CHANGE_HOME_DATA:
      const { topicList, articleList, recommendList } = payload;
      // 记得要改成 immutable 对象
      return state.merge({
        topicList: fromJS(topicList),
        articleList: fromJS(articleList),
        recommendList: fromJS(recommendList)
      });
    case constants.ADD_ARTICLE_LIST:
      const { list, nextPage } = payload;
      return state.merge({
        articleList: state.get('articleList').concat(list),
        articlePage: nextPage
      })
    case constants.CHANGE_BACKTOP_SHOW:
      return state.set('showBackTop', payload);
    default:
      return state;
  }
}

export default reducer;